package org.madao.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import lombok.Data;
import org.madao.gulimall.order.vo.PayVo;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2016101900721467";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCIu+3SuwxFazJ3/pM1FJBvPtpKIC+ZCwsXS1a+0EyFPAYhzCPncrp1C2vQrPTI24EepTkUF0JVlEr+xIHsk5mwl8Ccr/bsLXKvm+EJZwI9u1nTGd1FBUxVeaIfDaVeRpxgiRciDwtl5isNUJd+Syd8T5uecYMuKRNX3oOEZtPqR6uDBKMSYU8ShmXnOntMYx0zZCWR9fbU8cVyFVmqjHqgTG7loDgvlnK64tEpfxvmrTgpzA6RJA8CenDtn3QvIXfscRNVBB1MvSipXjE4qbEtt6DzQ2Rc7YU2rB+PRIS9+0wKma0XdBixqkyEg00fdZus9+buDL0XzBhkfaYEgK1pAgMBAAECggEAMA6tJdWsGHH2yQnmv088/HyAyHDLieTBvywxjtszKdjQ5uwS6nP+aMjc2fkQB9Bub1Du4MrT+7QlL6ntaDLwX4rWnbuS86t9lY8Z5u4XcY75oSKEDJAyJhCcFIH9T0dOCvfj84qKHQRquUFMHY1YkW6BaYzH6F9zdvy0CMMDVGHZ4ZCgTkBw9I1Ougdkg0dFek66p1aWq8Rt1hIFJoDJjZmzSO9OLiwQ37aOVaLXC8ZoiNH5J9IWaPHlPwfOBkF7xCYguXAB1HbBFrelvqRPAWTZEb3oqYFMQsGy4Y37Y7yRjSyd7myYKUHB5brCTj4Tyo3qhj0liRWzYa4MWwsOCQKBgQDmNaw4fLBj6KZU+66Lg81sQtPLISXKIqxKsz/nf7HqQ9YUvAePl7IoOUK9M4k20V+wVl4kCz5R1gvt1KQv0hAXzCzQ9RRshidBgo4GwoCXZ4V9VBA56pl9640Aj8mWEsx8/ZphcywEom2ahU0u0Znd9EiUH/fhRhNQdSq6NkjJxwKBgQCYDWl3d5u4zlwP3EwAvG2sHvORhVw7skJZh/+1x3kX7O4yH256yYYwMNrki9pp2CMuyf/BHVKytwUa0NFVUqX94d+wpxotKI9lJVaBv2xpOG1fJFW0Iib5lAH2Qyen2nZYsIEvBc/t8KyqzP20CwllpaOCJiFacxsLvLeRtApPTwKBgBQqjXyhR55o6fE13vNvx1X9gKohrDrJ0Mf2bdmssPwxlxZX5l2ozz2AzpzeX93Cd5xt5G9J4NqwYnkiEIPz5d7/zsk7Pt/G3Ld7ZU+oSDV7JxF0hLteFrYWJVLwRZt06B7m5Fg3C5sYU3uew8UfLVhpJtZ6+m7sgZKhwO1xBe1PAoGADkQr4hap2LQb9S0BQIxtadnZlozBs6QtD4NwHZkipnkHfFJwpjqkSX87+neYvZeW7XvscC0VJkl/xB1VlsDTbDe2Kjfzf8svcRkIvdG9FT5DHMsm82jE6WPWr0lrvhcERZwB8uyDrtqHCNuE7lCeyBe7eKmlNXxZTEefnZ8b3IcCgYEAol5LJueUJoGAelvDbe8pJrAF4r56dytnFoSoZS9IpQDeLCsEatq9iINKhGuB8gMuS5Ytq2buatj96v2c8N0HEkDHTz9BqHIOI5QpN8xpDHqX4cEsKXeYVIrZKfO4dztC1XE4Fdfe/0eacsP7uPOymZthAp6Clvp15+LRD6PObHA=";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAjpCWRVGj+Y9c27SOs/kO2XO+QEbD7FEZZwZ/Cy50i6dE97eXLPN9jQar9D0KtLGHMVv3QsAm+/PM3LKZIQnTx4w0jjvO5x46ubMWs+WnZthAEyvOFmq7j9Xeoyk3Vyzp1vECh0n3AxaLnjJKf/dGw6apk6Vk0pNJ263x0rL2l4P2+4y4zUeH4V+WdnOlefsHHV5SSGDunw02lUHMPwzogP34cxYFNEnaaeFgsnwC04PCafjqmp003s9COR2ynKeSO7JjEmAjfvjq6U66zhGeir4vh7v+M4yVKKHUMGkYQfoOu2biWO0SfPuHuNbXmP4V8hoC4tU1leHr4p3rNW5wpwIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url="http://4c346h.natappfree.cc/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url="http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    private String timeout = "30m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+ timeout +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
